using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using SouthernLand.Common.BOL.NhaDat;

namespace SouthernLand.Common.DAL.NhaDat
{
    public class DonVTDAL : BaseDAL
    {
        #region ObjIDataReader

        /// <summary>
        /// Returns a new DonVT instance filled with the DataReader's current record data 
        /// </summary>
        private DonVT GetDonVTFromReader(IDataReader dr)
        {
            var obj = new DonVT();
            obj.DonVTID = (dr["DonVTID"] is DBNull) ? Int32.MinValue : (Int32) dr["DonVTID"];
            obj.TenDonVT = (dr["TenDonVT"] is DBNull) ? String.Empty : (String) dr["TenDonVT"];
            obj.TenDonVT_en = (dr["TenDonVT_en"] is DBNull) ? String.Empty : (String) dr["TenDonVT_en"];
            return obj;
        }

        #endregion

        /// <summary>
        /// Returns a collection of DonVT with the data read from the input DataReader
        /// </summary>
        private List<DonVT> GetDonVTCollectionFromReader(IDataReader dr)
        {
            var obj = new List<DonVT>();
            while (dr.Read())
                obj.Add(GetDonVTFromReader(dr));
            return obj;
        }

        public int Insert(DonVT obj)
        {
            using (SqlConnection myConn = GetConnectionString())
            {
                var myCommand = new SqlCommand("w4u_DonVT_Insert", myConn);
                myCommand.CommandType = CommandType.StoredProcedure;
                myCommand.Parameters.Add("@DonVTID", SqlDbType.Int).Value = obj.DonVTID;
                myCommand.Parameters.Add("@TenDonVT", SqlDbType.NVarChar).Value = obj.TenDonVT;
                myCommand.Parameters.Add("@TenDonVT_en", SqlDbType.NVarChar).Value = obj.TenDonVT_en;

                return myCommand.ExecuteNonQuery();
            }
        }

        public bool Update(DonVT obj)
        {
            using (SqlConnection myConn = GetConnectionString())
            {
                var myCommand = new SqlCommand("w4u_DonVT_Update", myConn);
                myCommand.CommandType = CommandType.StoredProcedure;
                myCommand.Parameters.Add("@DonVTID", SqlDbType.Int).Value = obj.DonVTID;
                myCommand.Parameters.Add("@TenDonVT", SqlDbType.NVarChar).Value = obj.TenDonVT;
                myCommand.Parameters.Add("@TenDonVT_en", SqlDbType.NVarChar).Value = obj.TenDonVT_en;

                int ret = myCommand.ExecuteNonQuery();
                return (ret == 1);
            }
        }

        public bool Delete(Int32 DonVTID)
        {
            using (SqlConnection myConn = GetConnectionString())
            {
                var myCommand = new SqlCommand("w4u_DonVT_Delete", myConn);
                myCommand.CommandType = CommandType.StoredProcedure;
                myCommand.Parameters.Add("@DonVTID", SqlDbType.Int).Value = DonVTID;

                int ret = myCommand.ExecuteNonQuery();
                return (ret == 1);
            }
        }

        /// <summary>
        /// Retrieves the w4u_DonVT with the specified ID
        /// </summary>
        public DonVT GetDonVTByID(Int32 DonVTID)
        {
            using (SqlConnection myConn = GetConnectionString())
            {
                var myCommand = new SqlCommand("w4u_DonVT_GetDonVTByID", myConn);
                myCommand.CommandType = CommandType.StoredProcedure;
                myCommand.Parameters.Add("@DonVTID", SqlDbType.Int).Value = DonVTID;

                SqlDataReader dr = myCommand.ExecuteReader();
                return (dr.Read() ? GetDonVTFromReader(dr) : null);
            }
        }

        //Get All DonVT
        public List<DonVT> GetAllDonVT(int PageIndex, int PageSize)
        {
            using (SqlConnection myConn = GetConnectionString())
            {
                var myCommand = new SqlCommand("w4u_DonVT_GetAllDonVT", myConn);
                myCommand.CommandType = CommandType.StoredProcedure;
                myCommand.Parameters.Add("@PageIndex", SqlDbType.Int).Value = PageIndex;
                myCommand.Parameters.Add("@PageSize", SqlDbType.Int).Value = PageSize;
                SqlDataReader dr = myCommand.ExecuteReader();
                return GetDonVTCollectionFromReader(dr);
            }
        }

        // Get Count All records of Table  DonVT
        public int GetCountDonVT()
        {
            using (SqlConnection myConn = GetConnectionString())
            {
                var myCommand = new SqlCommand("w4u_DonVT_GetCountDonVT", myConn);
                myCommand.CommandType = CommandType.StoredProcedure;
                return (int) myCommand.ExecuteScalar();
            }
        }
    }
}